//
//  WBTabBarViewController.swift
//  SinaWB
//
//  Created by 财神软件 on 17/2/7.
//  Copyright © 2017年 财神软件. All rights reserved.
//

import UIKit

class WBTabBarViewController: UITabBarController {

    lazy var centerButton:UIButton = {
        var button = UIButton()
        button.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: .normal)
        button.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), for: .normal)
        button.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        button.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        return button
        
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        let viewW = UIApplication.shared.windows.first
        viewW?.isHidden=false
        print("--------",viewW?.isHidden,viewW)

        let VCarray = [
            ["title":"首页","image":"tabbar_home","ViewController":"WBHomeViewController"],
            ["title":"发现","image":"tabbar_discover","ViewController":"WBDiscoverViewController"],
            ["":""],
            ["title":"消息","image":"tabbar_message_center","ViewController":"WBMessageViewController"],
            ["title":"我的","image":"tabbar_profile","ViewController":"WBMyViewController"]]
        for obj in VCarray {
            CreatViewController(VCDic: obj)
        }
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tabBar.addSubview(centerButton)
        let W = tabBar.bounds.width/CGFloat(5)
        
        centerButton.frame=tabBar.bounds.offsetBy(dx: W*2, dy: 0)
        centerButton.width=W

    }
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask
        {
        return .portrait
    }
}


extension WBTabBarViewController{
    func CreatViewController(VCDic:[String:String]) -> () {
        guard let titlestr=VCDic["title"],let image=VCDic["image"],let VCstring=VCDic["ViewController"] ,let clsVC=NSClassFromString(Bundle.main.nameSpace+"."+VCstring) as? UIViewController.Type else {
            addChildViewController(WBNavigationController.init(rootViewController: UIViewController()))
            return
        }
        let VC = clsVC.init()
        VC.title=titlestr
        VC.view.backgroundColor=UIColor.init().randomColor
        VC.tabBarItem.image=UIImage.init(named: image)
        VC.tabBarItem.selectedImage=UIImage.init(named: image+"_selected")?.withRenderingMode(.alwaysOriginal)
        VC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        addChildViewController(WBNavigationController.init(rootViewController: VC))
    }
    
}
